import { regs, regTest } from './regs'

/** 二、业务数据类型
 * @param {Object} v 判断对象
 * @returns {Boolean}
 */
export const isMobile = (v) => regTest(regs.mobile, v)
export const isName = (v) => regTest(regs.name, v)
export const isQQ = (v) => regTest(regs.qq, v)
export const isCreditId = (v) => {
	const arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
	const arrValid = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] // 校验码
	let y, m, d
	const funIsValidDate = function (y, m, d) {
		const now = new Date(y, m - 1, d, 12, 0, 0)
		const _y = now.getFullYear()
		const _m = now.getMonth() + 1
		const _d = now.getDate()
		return +y === _y && +m === _m && +d === _d
	}
	if (/^\d{15}$/.test(v)) {
		y = '19' + v.substr(6, 2)
		m = v.substr(8, 2)
		d = v.substr(10, 2)
		return funIsValidDate(y, m, d)
	} else if (/^\d{17}\d|x$/i.test(v)) {
		let sum = 0
		for (let i = 0; i < v.length - 1; i++) {
			sum += parseInt(v.substr(i, 1), 10) * arrExp[i]
		}
		const vBit = sum % 11
		y = v.substr(6, 4)
		m = v.substr(10, 2)
		d = v.substr(12, 2)
		return arrValid[vBit] === v.substr(17, 1).toUpperCase() && funIsValidDate(y, m, d)
	} else {
		return false
	}
}
